package sat.frame;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import org.mt4j.MTApplication;
import org.mt4j.components.TransformSpace;
import org.mt4j.components.visibleComponents.shapes.MTPolygon;
import org.mt4j.input.inputProcessors.globalProcessors.CursorTracer;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import sat.component.ClassComponent;
import sat.component.Toolbar;

//this class isn't singleton
public class WorkPlaceScene extends AbstractScene implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Toolbar toolbarLeft;
	public Toolbar getToolbarLeft() {
		return toolbarLeft;
	}

	public Toolbar getToolbarRight() {
		return toolbarRight;
	}

	private Toolbar toolbarRight;

	//
	private static WorkPlaceScene instance;
	public static WorkPlaceScene getInstance(){
		return instance;
	}

	/*
	 * class list
	 */

	private List<ClassComponent> listClass = new LinkedList<ClassComponent>();

	public List<ClassComponent> getListClass() {
		return listClass;
	}

	public void addClassToList(ClassComponent c){
		listClass.add(c);
	}

	public ClassComponent findClassComponentByPosition(Vector3D pos){
		ClassComponent res = null;

		//TODO two class intersection
		for (int i = 0; i < listClass.size(); i++) {
			Vector3D p = ((ClassComponent)listClass.get(i)).globalToLocal(pos);
			if (((ClassComponent)listClass.get(i)).isGeometryContainsPointLocal(p)){
				res = (ClassComponent)listClass.get(i);
				break;
			}
		}

		return res;
	}

	/*
	 *  use case people component list
	 */

	private List<MTPolygon> listActor = new LinkedList<MTPolygon>();

	public List<MTPolygon> listActor() {
		return listActor;
	}

	public void addActorToList(MTPolygon c){
		listActor.add(c);
	}

	/*public MTPolygon findUseCasePeopleComponentByPosition(Vector3D pos){
		MTPolygon res = null;

		//TODO two class intersection
		System.out.println("size = "+listPeople.size());
		for (int i = 0; i < listPeople.size(); i++) {
			Vector3D p = ((MTPolygon)listPeople.get(i)).globalToLocal(pos);
			if (((MTPolygon)listPeople.get(i)).isGeometryContainsPointLocal(p)){
				res = (MTPolygon)listPeople.get(i);
				break;
			}
		}
		return res;
	}*/

	/*
	 *  use case association component list
	 */

	private List<MTPolygon> listAsso = new LinkedList<MTPolygon>();

	public List<MTPolygon> listAsso() {
		return listAsso;
	}

	public void addAssociationToList(MTPolygon c){
		listAsso.add(c);
	}

	public MTPolygon findUseCaseAssociationComponentByPosition(Vector3D pos){
		MTPolygon res = null;

		//TODO two class intersection
		System.out.println("size = "+listAsso.size());
		for (int i = 0; i < listAsso.size(); i++) {
			Vector3D p = ((MTPolygon)listAsso.get(i)).globalToLocal(pos);
			if (((MTPolygon)listAsso.get(i)).isGeometryContainsPointLocal(p)){
				res = (MTPolygon)listAsso.get(i);
				break;
			}
		}
		return res;
	}

	public WorkPlaceScene(final MTApplication mtApplication, final String name) {
		super(mtApplication, name);
		instance = this;
		this.getCanvas().setFrustumCulling(false);
		this.setClearColor(new MTColor(210, 215, 193, 255));
		this.registerGlobalInputProcessor(new CursorTracer(mtApplication, this)); // show touch point
		toolbarLeft = new Toolbar(mtApplication,this);
		toolbarLeft.setPositionRelativeToParent(new Vector3D(mtApplication.width/4,
				toolbarLeft.getHeightXY(TransformSpace.LOCAL)/2+Toolbar.tabListHeight+50));
		toolbarRight = new Toolbar(mtApplication,this);
		toolbarRight.setPositionRelativeToParent(new Vector3D(mtApplication.width*3/4,
				toolbarLeft.getHeightXY(TransformSpace.LOCAL)/2+Toolbar.tabListHeight+50));

		this.getCanvas().addChild(toolbarLeft);
		this.getCanvas().addChild(toolbarRight);
	}

	public void init() { }

	public void shutDown() {}
}
